package tt.com.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tt.com.business.mapper.DeadMapper;
import tt.com.business.service.*;
import tt.com.business.vo.InformationVo;
import tt.com.common.enums.Status;
import tt.com.common.enums.TollEnum;
import tt.com.common.model.Dead;
import tt.com.common.model.DeadProject;
import tt.com.common.model.YxCommonServeProviders;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DeadServicelmpl extends ServiceImpl<DeadMapper, Dead> implements DeadService {
   //引入service

    //逝者家属信息
    @Autowired
    private DeadRelationService deadRelationService;
    //逝者服务信息
    @Autowired
    private DeadProjectService projectService;
    //服务项目表
    @Autowired
    private CommonServeProvidersService commonServeProvidersService;
    //整容
    @Autowired
    private DeadCosmeticService deadCosmeticService;
    //告别
    @Autowired
    private DeadHallService deadHallService;

    //冷藏
    @Autowired
    private ColdRoomService coldRoomService;

    //火化
    @Autowired
    private CremateService cremateService;



    //查询逝者信息
    @Override
    public PageUtil selectDead(PageUtil pageUtil) {
        IPage<Dead> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Dead> qw=new QueryWrapper<>();
        qw.eq("data_status",1);
        IPage<Dead> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        QueryWrapper<Dead> qwe=new QueryWrapper<>();
        qw.eq("data_status",1);
        pageUtil.setTotalSize(baseMapper.selectCount(qwe));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());
        return pageUtil;
    }
    //根据ID逻辑删除/修改逝者信息
    @Override
    public ResponseEntity deleteDead(Dead dead) {
        if (dead==null){
            return new ResponseEntity("信息不可以为空",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        int i = baseMapper.updateById(dead);
        if (i<0){
            return new ResponseEntity("操作失败",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity("操作成功",HttpStatus.OK);
    }

    //添加逝者信息,家属信息，服务信息，批量添加服务项目,分配业务
    @Override
    public ResponseEntity addDead(InformationVo informationVo) {
        String id = informationVo.getDead().getIdCard();
        QueryWrapper<Dead> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card",id);
        List<Dead> deads = baseMapper.selectList(queryWrapper);
        if (deads.size()>0){
            return new ResponseEntity("该逝者已经填写过了",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if (!ObjectUtil.objectIsNull(informationVo.getDead())) {
            return new ResponseEntity("逝者信息不全",HttpStatus.INTERNAL_SERVER_ERROR);
        }

        if (!ObjectUtil.objectIsNull(informationVo.getDeadRelation())) {
            return new ResponseEntity("逝者家属信息不全",HttpStatus.INTERNAL_SERVER_ERROR);
        }


        //查询逝者表的数量
        Integer integer = baseMapper.selectCount(null);
        //设置逝者信息的id
        informationVo.getDead().setId(String.valueOf(integer+1));
        //设置死者编号
        informationVo.getDead().setDeadNo(informationVo.getDead().getId());
        //添加逝者信息
        int insert = baseMapper.insert(informationVo.getDead());
        informationVo.getDeadRelation().setDeadId(informationVo.getDead().getId());


        //查询家属信息的总数
        int count = deadRelationService.count();
        informationVo.getDeadRelation().setId(String.valueOf(count+1));
        //获取逝者ID
        informationVo.getDeadRelation().setDeadId(informationVo.getDead().getId());
        //添加逝者家属信息
        int i = deadRelationService.addRelation(informationVo.getDeadRelation());

        //添加服务信息
        //产生批量的服务订单
        List<DeadProject> list=new ArrayList<>();
        if (informationVo.getCommonServeProvidersList().size()<0){
            return new ResponseEntity("请选择你要的服务",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        List<YxCommonServeProviders> commonServeProvidersList = informationVo.getCommonServeProvidersList();


        //逝者查询服务总数
        int count1 = projectService.count();
        for (YxCommonServeProviders yxCommonServeProviders : commonServeProvidersList) {
                DeadProject deadProject=new DeadProject();
                count1++;
                deadProject.setId(String.valueOf(count1));
                //设置逝者id
                deadProject.setDeadId(informationVo.getDead().getId());
                //设置服务类型id
                deadProject.setProjectId(yxCommonServeProviders.getServeTypeId());
                //获取价格
                BigDecimal bigDecimal = commonServeProvidersService.selectPrice(yxCommonServeProviders.getServeTypeId());
                deadProject.setUnitPrice(bigDecimal);
                //设置缴费的状态
                deadProject.setChargeableStatus(TollEnum.UNPAID);
                //设置创建数据
                deadProject.setCreateTime(new Date());
                //设置删除状态
                deadProject.setDataStatus(1);
                list.add(deadProject);
            //


        }
        //
        boolean b = projectService.saveBatch(list);
        //整容
        for (YxCommonServeProviders yxCommonServeProviders : commonServeProvidersList) {
                if (yxCommonServeProviders.getServeTypeId().equals("1")||yxCommonServeProviders.getServeTypeId().equals("2")||yxCommonServeProviders.getServeTypeId().equals("3")){
                    if (!ObjectUtil.objectIsNull(informationVo.getDeadCosmetic())) {
                        return new ResponseEntity("项目信息填写不全",HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                    int count2 = deadCosmeticService.count();
                    //设置逝者id
                    informationVo.getDeadCosmetic().setDeadId(informationVo.getDead().getId());
                    //逝者整容表id
                    informationVo.getDeadCosmetic().setId(String.valueOf(count2+1));
                    //设置项目id
                    informationVo.getDeadCosmetic().setProjectId(yxCommonServeProviders.getId());
                    //设置状态
                    informationVo.getDeadCosmetic().setCosmeticStatus(0);
                    deadCosmeticService.save(informationVo.getDeadCosmetic());


                }
                //告别
            if (yxCommonServeProviders.getServeTypeId().equals("4")||yxCommonServeProviders.getServeTypeId().equals("5")){
                if (!ObjectUtil.objectIsNull(informationVo.getDeadHall())) {
                    return new ResponseEntity("项目信息填写不全",HttpStatus.INTERNAL_SERVER_ERROR);
                }
                int count2 = deadHallService.count();
                //设置逝者的id
                informationVo.getDeadHall().setDeadId(informationVo.getDead().getId());
                //设置id
                informationVo.getDeadHall().setId(String.valueOf(count2+1));
                //添加使用类型
                if (yxCommonServeProviders.getServeTypeId().equals("4")){
                    informationVo.getDeadHall().setUseType(1);
                }
                if (yxCommonServeProviders.getServeTypeId().equals("5")){
                    informationVo.getDeadHall().setUseType(2);
                }


                //添加业务表
                deadHallService.save(informationVo.getDeadHall());


            }
            //冷藏
            if (yxCommonServeProviders.getServeTypeId().equals("6")){
                if (!ObjectUtil.objectIsNull(informationVo.getDeadColdRoom())) {
                    return new ResponseEntity("项目信息填写不全",HttpStatus.INTERNAL_SERVER_ERROR);
                }
                int count2 = coldRoomService.count();
                //设置业务id
                informationVo.getDeadColdRoom().setId(String.valueOf(count2+1));
                //设置逝者的id
                informationVo.getDeadColdRoom().setDeadId(informationVo.getDead().getId());
                //设置冷藏状态
                informationVo.getDeadColdRoom().setRefrigerateStatus(1);
                //添加冷藏业务
                coldRoomService.save(informationVo.getDeadColdRoom());



            }
            //火化
            if (yxCommonServeProviders.getServeTypeId().equals("7")){
//                if (!ObjectUtil.objectIsNull(informationVo.getCremate())) {
//                    return new ResponseEntity("项目信息填写不全",HttpStatus.INTERNAL_SERVER_ERROR);
//                }
                int count2 = cremateService.count();
                //设置id
                informationVo.getCremate().setId(String.valueOf(count2+1));
                //设置逝者id
                informationVo.getCremate().setDeadId(informationVo.getDead().getId());
                //添加火化业务
                cremateService.save(informationVo.getCremate());




            }




        }



        if (!b||insert<0 || i<0){
            return new ResponseEntity("添加失败",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity("添加成功",HttpStatus.OK);
    }
}
